//
//  LoginView.m
//  项目三
//
//  Created by rimi on 15/9/29.
//  Copyright (c) 2015年 rimi. All rights reserved.
//
#define Width 400.0f
#define Height 400.0f

#import "LoginView.h"
#import <QuartzCore/QuartzCore.h>

@interface LoginView () {

    UILabel *_nameLabel;
    UIButton *_btn;
    UIButton *_registBtn;
}

@end

@implementation LoginView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.layer.cornerRadius = 8.0;
        self.layer.masksToBounds = YES;
        
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = DHFlexibleFrame(CGRectMake(0, 0, Width, Height));
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1);
        gradientLayer.colors = @[(__bridge id)COLOR(163, 163, 163, 1).CGColor,(__bridge id)COLOR(116, 116, 116, 1).CGColor];//
        [self.layer addSublayer:gradientLayer];
        
        [self addSubview:self.loginImage];
        [self addSubview:self.nameField];
        [self addSubview:self.passwordField];
        [self addSubview:self.yesBtn];
        [self addSubview:self.cancelBtn];
        
        self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
        
        _nameLabel = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(10, 167, Width, 25))];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.text = @"首次登陆,请输入您签约时的主卡卡号以及预留密码.";
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_nameLabel];
        
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn setBackgroundImage:[UIImage imageNamed:@"取回用户名"] forState:UIControlStateNormal];
        [_btn setFrame:DHFlexibleFrame(CGRectMake(252, 335, 135, 32))];
        [_btn addTarget:self action:@selector(registerButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btn];
        
        UIViewController *topVC = [self appRootViewController];
        self.frame = DHFlexibleFrame(CGRectMake((CGRectGetWidth(topVC.view.bounds) - Width) * 0.5, - Height - 30, Width, Height));
            [topVC.view addSubview:self];
        
    }
    return self;
}

- (UIViewController *)appRootViewController {
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}
#pragma mark - system methods
- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (newSuperview == nil) {
        return;
    }
    UIViewController *topVC = [self appRootViewController];
    
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:topVC.view.bounds];
        self.backImageView.backgroundColor = [UIColor blackColor];
        self.backImageView.alpha = 0.6f;
        self.backImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    [topVC.view addSubview:self.backImageView];
    CGRect afterFrame = DHFlexibleFrame(CGRectMake((CGRectGetWidth(topVC.view.bounds) - Width) * 0.5, (CGRectGetHeight(topVC.view.bounds) - Height) * 0.5, Width, Height));
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.transform = CGAffineTransformMakeRotation(0);
        self.frame = DHFlexibleFrame(CGRectMake((CGRectGetWidth(topVC.view.bounds) - Width) * 0.5, (CGRectGetHeight(topVC.view.bounds) - Height) * 0.5 + 100, Width, Height));
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.55f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            self.frame = DHFlexibleFrame(CGRectMake((CGRectGetWidth(topVC.view.bounds) - Width) * 0.5, (CGRectGetHeight(topVC.view.bounds) - Height) * 0.5 - 100, Width, Height));
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.85f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                self.frame = afterFrame;
                
            } completion:^(BOOL finished) {
                
            }];
        }];
    }];
    [super willMoveToSuperview:newSuperview];
}

- (void)removeFromSuperview {
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    UIViewController *topVC = [self appRootViewController];
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - Width) * 0.5, CGRectGetHeight(topVC.view.bounds), Width, Height);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.frame = afterFrame;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
}
#pragma mark - privite methods
- (void)rightButtonEvent:(UIButton *)sender {

    [self removeFromSuperview];
}
/**< 请求数据 */
- (void)leftButtonEvent:(UIButton *)sender {

    
}
- (void)cancelRegistEvent:(UIButton *)sender {

    [UIView transitionWithView:self duration:0.8 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
        
        [_registBtn removeFromSuperview];
        self.nameField.text = nil;
        self.passwordField.text = nil;
        self.yesBtn.hidden = NO;
        self.cancelBtn.hidden = NO;
        sender.hidden = YES;
        _nameLabel.hidden = NO;
        _btn.hidden = NO;
        self.loginImage.image = [UIImage imageNamed:@"卡账号_03"];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)RegistEvent:(UIButton *)sender {

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"服务器繁忙" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}
- (void)registerButtonEvent:(UIButton *)sender {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"登录-取消"] forState:UIControlStateNormal];
    [btn setFrame:DHFlexibleFrame(CGRectMake(225, 246, 143, 48))];
    [btn addTarget:self action:@selector(cancelRegistEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    
    _registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_registBtn setBackgroundImage:[UIImage imageNamed:@"登录-确定"] forState:UIControlStateNormal];
    [_registBtn setFrame:DHFlexibleFrame(CGRectMake(54, 246, 143, 48))];
    [_registBtn addTarget:self action:@selector(RegistEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_registBtn];
    
    [UIView transitionWithView:self duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        
        [self regist];
        
    } completion:^(BOOL finished) {
        
    }];
}
- (void)regist {
    
    self.nameField.text = nil;
    self.passwordField.text = nil;
    _btn.hidden = YES;
    _nameLabel.hidden = YES;
    self.loginImage.image = [UIImage imageNamed:@"预留手机_03"];
    self.yesBtn.hidden = YES;
    self.cancelBtn.hidden = YES;
}
#pragma mark - getter
- (UITextField *)nameField {

    if (!_nameField) {
        _nameField = [[UITextField alloc] initWithFrame:DHFlexibleFrame(CGRectMake(175, 56, 200, 36))];
        _nameField.textColor = [UIColor blackColor];
        _nameField.borderStyle = UITextBorderStyleNone;
    }
    return _nameField;
}
- (UITextField *)passwordField {

    if (!_passwordField) {
        _passwordField = [[UITextField alloc] initWithFrame:DHFlexibleFrame(CGRectMake(175, 114, 200, 36))];
        _passwordField.textColor = [UIColor blackColor];
        _passwordField.secureTextEntry = YES;
        _passwordField.borderStyle = UITextBorderStyleNone;
    }
    return _passwordField;
}
- (UIButton *)cancelBtn {

    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setFrame:DHFlexibleFrame(CGRectMake(225, 246, 143, 48))];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"登录-取消"] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(rightButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)yesBtn {

    if (!_yesBtn) {
        _yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_yesBtn setFrame:DHFlexibleFrame(CGRectMake(54, 246, 143, 48))];
        [_yesBtn setBackgroundImage:[UIImage imageNamed:@"登录-确定"] forState:UIControlStateNormal];
        [_yesBtn addTarget:self action:@selector(leftButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _yesBtn;
}
- (UIImageView *)loginImage {

    if (!_loginImage) {
        _loginImage = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(22, 51, 352, 102))];
        _loginImage.image = [UIImage imageNamed:@"卡账号_03"];
    }
    return _loginImage;
}
@end
